#include "Col.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "RefCounted.h"
#include "Class.h"
#include "ET_stdio.h"

//---- RefCounted --------------------------------------------------------------

NewMetaImpl(RefCounted,Object, (T(refcnt)));

RefCounted::RefCounted()
{
	refcnt= 0;
}

RefCounted::~RefCounted()
{
	if (refcnt > 0)
		fprintf(stderr, "RefCounted::~RefCounted: oops refcnt > 0\n");
}

void RefCounted::Unref()
{
	if (this) {
		if (refcnt > 0) {
			refcnt--;
			if (refcnt == 0)
				delete this;
		}
	} else
		fprintf(stderr, "RefCounted::Unref: this == 0\n");
}

void RefCounted::InitNew()
{
	Object::InitNew();
	refcnt= 0;
}

void RefCounted::Assign(RefCounted **to, RefCounted *from)
{
	if (to) {
		if (*to != from) {
			if (from)
				from->Ref();
			if (*to)
				(*to)->Unref();
			*to= from;
		}
	}
}


